package dsaa;

import java.io.*;

public class lab2fmy {

    public static void main(String[] args) throws IOException {
        //二分套二分A[i][j] i2+12345×i+j2−12345×j+i×j
        //当j固定，i=n时取最大值
        //外层二分：先找到假设的排第m位的数的值
        //内层二分：判断是否正确
        //数组会超内存思考不用数组的办法
        //找出最大的满足有m-1个数比其小的数（二分查找）
        StreamTokenizer input = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter print = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        input.nextToken();
        int t = (int) input.nval;
        while (t >= 1) {
            input.nextToken();
            int n = (int) input.nval;
            input.nextToken();
            long m = (long) input.nval - 1;

            //外层二分
            long smallerNum = -1;
            long left = Long.MIN_VALUE + 1;
            long right = Long.MAX_VALUE - 1;
            long mid = 0;
            long answer = 0;
            while (smallerNum != m) {
                long[] result = countSmallerNum(mid, n);
                smallerNum = result[0];
                answer = result[1];
                if (smallerNum < m) {
                    // 数字偏小 大一点
                    left = mid + 1;
                } else if (smallerNum > m) {
                    // 数字偏大 小一点
                    right = mid - 1;
                } else {
                    break;
                }
                mid = (left + right) / 2;
            }
            print.println(answer);
            print.flush();
            t = t - 1;
        }
    }

    /**
     * 二分查找是否构成答案
     * 0: 前面有x个比它小的数
     * 1: 当前答案
     */
    public static long[] countSmallerNum(long ans, long n) {
        long count = 0;
        long j = 1;
        long[] result = new long[2];
        long resultNum = Long.MAX_VALUE;
        while (j <= n) {
            //找到j固定情况下，比mnum小的数字的下标的最大值，所以等于的情况下，还要往后找
            long l = 1;
            long r = n;
            while (l <= r) {
                long mid = (l + r) / 2;
                if (fx(mid, j) > ans) {
                    r = mid - 1;
                } else if (fx(mid, j) == ans){
                    l = mid;
                    break;
                } else {
                    l = mid + 1;
                }
            }
            if (l <= n) {
                long temp = fx(l, j);
                resultNum = Math.min(resultNum, temp);
            }
            // 如果找不到, l-1的位置是比ans小的数字下标的最大值
            count = count + l - 1;
            j = j + 1;
        }
        result[0] = count;
        result[1] = resultNum;
        return result;
    }

    public static long fx(long i, long j) {
        return i * i + 12345L * i + j * j - 12345L * j + i * j;
    }

}
